/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the squared_length algorithm.

#ifndef __sgl_squared_length_h__
#define __sgl_squared_length_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <sgl/line_segment.h>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* squared_length                                                             */
/*                                                                            */
/******************************************************************************/

template <
	/// The geometric primitive. 
	typename E>
/// Get the squared length.
//!
//! This function calculates the squared length of a geometric primitive.
//!
//! /return The squared length of the geometric primitive.
inline typename E::value_type squared_length(
	E const& primitive)
{
	E::value_type squared_length = E::value_type();
	for (size_t i=0; i!=primitive.dimensions(); ++i)
		squared_length += (primitive[i]) * (primitive[i]);	// (*) see below
	return squared_length;
}

// (*) If you get an error in this line, this means that your type does not
//     support operator [] (). Most likely, this means that length calculation
//     is not suitable for the type. However, if it is suitable, you should
//     provide an overload of the squared_length algorithm, which is able to
//     work with your type.

template <
	/// The dimensionality. 
	size_t D,
	/// The element type. 
	typename T>
/// Get the squared length.
//!
//! This function calculates the squared length of a line_segment.
//!
//! /return The squared length of the geometric primitive.
inline typename T squared_length(
	sgl::line_segment<D,T> const& primitive)
{
	T squared_length = primitive.extent() * primitive.extent();
	return squared_length;
}


} // namespace sgl

#endif // __sgl_squared_length_h__
